package com.forum.controller;

import com.forum.pojo.Message;
import com.forum.pojo.User;
import com.forum.pojo.Evaluate;
import com.forum.service.MessageService;
import com.forum.tool.forMessageID;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;


@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    //添加
    @RequestMapping(value = "/addMessage",produces = "text/html;charset=UTF-8")
    public String addMessage(Message message, Model model, HttpSession session){


        //SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        //System.out.println(formatter.format(date));
        message.setPostingTime(date);
        //生成帖子ID
        forMessageID f = new forMessageID();
        message.setMessageID(f.getMeaasgeID());
        User u = (User)session.getAttribute("sessionUser");
        message.setUserID(u.getUserID());
        message.setAuditSatus("未审核");
        messageService.addMessager(message);

        model.addAttribute("messageData",message);
        return "message";
    }

    //查找所有
    @RequestMapping(value = "/findMessageById",produces = "text/html;charset=UTF-8")
    public String findMessageById(String flag, String messageID, Model model){
        if(flag!=null){
            model.addAttribute("update","update");
        }

        Message messageData = messageService.findMessageById(messageID);
        model.addAttribute("messageData",messageData);
        List<Evaluate> evaluateList = messageService.findEvaluate(messageID);
        model.addAttribute("eList",evaluateList);
        return "message";
    }


    //按文章标题模糊查询
    @RequestMapping(value = "/findMessageByName",produces = "text/html;charset=UTF-8")
    public String findMessageByName(String messageName ,@RequestParam(value="pageNum",required = false,defaultValue = "0")int pageNum, Model model) {
        if (pageNum != 0) {
            PageHelper.startPage(pageNum, 5);//第几页和一页多少个帖子
        }
        System.out.println();
        List<Message> messageList = messageService.findMessageByMessageName(messageName);
        model.addAttribute("messageList", messageList);
        for (Message u : messageList) {
            System.out.println(u.getMessageID());
        }
        return "index";
    }

    //按查询
    @RequestMapping(value = "/findMessageByModule",produces = "text/html;charset=UTF-8")
    public String findMessageByModule(String moduleName ,@RequestParam(value="pageNum",required = false,defaultValue = "0")int pageNum, Model model) {
        if (pageNum != 0) {
            PageHelper.startPage(pageNum, 4);//第几页和一页多少个帖子
        }
        System.out.println();
        List<Message> messageList = messageService.findMessageByModuleName(moduleName);
        model.addAttribute("messageList", messageList);
        for (Message u : messageList) {
            System.out.println(u.getMessageID());
        }
        return "index";
    }


        //查找所有
    @RequestMapping(value = "/allMessage",produces = "text/html;charset=UTF-8")
    public String allUser(@RequestParam(value="pageNum",required = false,defaultValue = "0")int pageNum, Model model) {
        if (pageNum != 0) {
            PageHelper.startPage(pageNum, 4);//第几页和一页多少个帖子
        }

        List<Message> messageList = messageService.findAllMessage();
        model.addAttribute("messageList", messageList);
        for (Message u : messageList) {
            System.out.println(u.getMessageID());
        }
        return "index";
    }



    //
    @RequestMapping(value = "/deleteMessage",produces = "text/html;charset=UTF-8")
    public String deleteUser(String messageID, Model model,HttpSession session){
        if(messageService.deleteMessage(messageID)==1){
            //删除成功
            User userData = (User)session.getAttribute("sessionUser");
            List<Message> messageList = messageService.findMessageByUserID(userData.getUserID());
            model.addAttribute("messageList", messageList);
            return "myMessage";
        }
        return "myMessage";
    }

    //
    @RequestMapping(value = "/findMessageByUserID",produces = "text/html;charset=UTF-8")
    public String findMessageByUserID(@RequestParam(value="pageNum",required = false,defaultValue = "0")int pageNum,String userID ,Model model){
        if (pageNum != 0) {
            PageHelper.startPage(pageNum, 4);//第几页和一页多少个帖子
        }
        List<Message> messageList = messageService.findMessageByUserID(userID);
        model.addAttribute("messageList", messageList);
        return "myMessage";
    }

    //
    @RequestMapping(value = "/updateMessage",produces = "text/html;charset=UTF-8")
    public String updateMessage(String messageID,String messageContent ,String moduleName,Model model){
        System.out.println("前");
        messageService.updateMessage(messageContent,moduleName,messageID);
        System.out.println(messageService.updateMessage(messageContent,moduleName,messageID));
        System.out.println("后");
        Message messageData = messageService.findMessageById(messageID);
        model.addAttribute("messageData",messageData);
        List<Evaluate> evaluateList = messageService.findEvaluate(messageID);
        model.addAttribute("eList",evaluateList);
        return "message";
    }

    @RequestMapping(value = "/addEvaluate",produces = "text/html;charset=UTF-8")
    public String addEvaluate(String messageID,String userID,String comments,Model model){
        messageService.addEvaluate(messageID,userID,comments);
        Message messageData = messageService.findMessageById(messageID);
        model.addAttribute("messageData",messageData);
        List<Evaluate> evaluateList = messageService.findEvaluate(messageID);
        model.addAttribute("eList",evaluateList);
        return "message";
    }

}
